<!-- el-form如何动态添加、删除输入框并每一项带验证 -->
<template>
  <ElConfigProvider :locale="locale">
    <div class="form-container">
      <el-form :model="formData" :rules="rules" ref="formRef" label-width="100px" class="user-form">
        <h2 class="form-title">用户信息表单</h2>
        <el-row>
          <template v-for="(item, i) in formData.cellConfigEntitieList" :key="i">
            <el-col :span="5">
              <el-form-item
                :label="'列表名'"
                :prop="'cellConfigEntitieList[' + i + '].titleName'"
                :rules="rules.titleName"
              >
                <el-input v-model="item.titleName" placeholder="请输入列表名" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item
                :label="'列表规则'"
                :prop="'cellConfigEntitieList[' + i + '].compareSymbols'"
                :rules="rules.compareSymbols"
              >
                <el-input v-model="item.compareSymbols" placeholder="请输入列表符号" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item
                :label="'列表值'"
                :prop="'cellConfigEntitieList[' + i + '].compareValue'"
                :rules="rules.compareValue"
              >
                <el-input v-model="item.compareValue" placeholder="请输入列表值" />
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item
                :label="'列表颜色'"
                :prop="'cellConfigEntitieList[' + i + '].colour'"
                :rules="rules.colour"
              >
                <el-input v-model="item.colour" placeholder="请输入列表颜色" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <div class="setting-btn">
                <el-button :icon="Plus" type="primary" size="small" circle @click="addCell" />
                <el-button
                  :icon="Minus"
                  type="primary"
                  size="small"
                  circle
                  @click="deleteCell(i)"
                />
              </div>
            </el-col>
          </template>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="备注" prop="notes">
              <el-input
                v-model="formData.notes"
                placeholder="请输入备注"
                type="textarea"
                :rows="3"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-button type="primary" @click="submitForm">提交</el-button>
              <el-button @click="resetForm">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </ElConfigProvider>
</template>

<script setup>
import { ref } from 'vue'
import { ElConfigProvider, ElMessage } from 'element-plus'
import { Plus, Minus } from '@element-plus/icons-vue'
// 引入的是中文语言包
import zhCn from 'element-plus/es/locale/lang/zh-cn'
const formData = ref({
  cellConfigEntitieList: [
    {
      titleName: '',
      colour: '',
      compareSymbols: '',
      compareValue: ''
    }
  ],
  notes: null // 备注
})

const formRef = ref(null)

const locale = ref(zhCn)

const rules = {
  // 验证规则是一个数组，因为可以设置多条验证规则
  // 每一条验证规则，是一个对象
  titleName: [{ required: true, message: '请输入列表名', trigger: ['blur', 'change'] }],
  compareSymbols: [{ required: true, message: '请输入列表规则', trigger: ['blur', 'change'] }],
  compareValue: [{ required: true, message: '请输入列表值', trigger: ['blur', 'change'] }],
  colour: [{ required: true, message: '请输入列表颜色', trigger: ['blur', 'change'] }]
}
const addCell = () => {
  formData.value.cellConfigEntitieList.push({
    titleName: '',
    colour: '',
    compareSymbols: '',
    compareValue: ''
  })
}
const deleteCell = (index) => {
  if (formData.value.cellConfigEntitieList.length === 1) {
    ElMessage({
      message: '列表不能为空',
      type: 'error'
    })
    return
  }
  formData.value.cellConfigEntitieList.splice(index, 1)
}
// 提交表单
function submitForm() {
  // 提交表单的时候，需要看一下表单是否验证通过
  formRef.value.validate((valid) => {
    // 自动传入一个参数，这个参数是一个布尔值，代表表单是否验证通过
    if (valid) {
      // 验证通过
      console.log(formData.value)
      console.log('表单验证通过')
    } else {
      // 验证不通过
      console.log('表单验证不通过')
      return false
    }
  })
}
</script>

<style scoped>
.form-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

.user-form {
  width: 1100px;
  border: 1px solid #eee;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}
.setting-btn {
  margin-left: 12px;
  margin-top: 4px;
}
.form-title {
  text-align: center;
  margin-bottom: 20px;
}
</style>
